package com.jifeng.sterilizer.service.impl;

import com.jifeng.sterilizer.entity.LiquidEntity;
import com.jifeng.sterilizer.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;

import com.jifeng.sterilizer.dao.LiquidTypeDao;
import com.jifeng.sterilizer.entity.LiquidTypeEntity;
import com.jifeng.sterilizer.service.LiquidTypeService;


@Service("liquidTypeService")
@Slf4j
public class LiquidTypeServiceImpl extends ServiceImpl<LiquidTypeDao, LiquidTypeEntity> implements LiquidTypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<LiquidTypeEntity> queryWrapper = new QueryWrapper<>();
        Object name = params.get("name");
        if (!name.equals("")) {
            queryWrapper.like("name", name);
        }
        IPage<LiquidTypeEntity> page = this.page(
                new Query<LiquidTypeEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R del(Map<String, Object> params) {

        Object id = params.get("id");
        boolean remove = this.removeById((Serializable) id);
        if (remove) {
            return R.ok();
        }
        return R.error(400, "无效的药剂类型id");
    }

    @Override
    public R getInfoByName(Map<String, Object> params) {
        try {
            Object name = params.get("name");
            List<LiquidTypeEntity> liquidTypeEntities = this.list(new QueryWrapper<LiquidTypeEntity>().like("name", name));
            if (liquidTypeEntities.size() > 10) {
                liquidTypeEntities.subList(0, 10);
            }
            return R.ok().put("data", liquidTypeEntities);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "根据名称模糊查询药剂类型", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

}